<?php

namespace app\hotel\controller;

use think\Request;
use app\hotel\model\Hotel as hotelModel;
use think\Session;
use think\Db;
use app\common\model\HotelRoomModel as hrm;

vendor('qiniu/php-sdk/autoload.php');

use Qiniu\Auth;
use Qiniu\Storage\UploadManager; //实例化上传类

class Hotel extends Common
{

    public function hotel()
    {

        $hotel_id = Session::get('hotel_id');
        $hotel = hotelModel::getHotelById($hotel_id);

        $this->assign('hotel', $hotel);

        return view();

    }

    public function shouldknow()
    {
        $hotel_id = Session::get('hotel_id');
        $hotel = hotelModel::getHotelById($hotel_id);
        //city address latest_time
        if (!empty($hotel)) {
            $hotel['time'] = substr($hotel['latest_time'], 0, 2);
        }
        $this->assign('hotel', $hotel);

        return view();

    }

    /**
     * 修改预定须知里面的字段
     */
    public function upShouldKnow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $hotel_id = Session::get('hotel_id');
            if (!empty($post['address'])) {
                $up['address'] = $post['address'];
            }
            if (!empty($post['latest_time'])) {
                $up['latest_time'] = $post['latest_time'];
            }
            $up['city'] = "山东省临沂市沂水县";

            $data = Db::name("hotel")
                ->where('hotel_id', $hotel_id)
                ->update($up);
            if ($data) {
                return json(['code' => 0, 'data' => "修改成功"]);

            } else {
                return ['code' => 1, 'message' => '修改失败'];
            }
        }
    }

    /**
     * [getRoomDetail 获取房间的详情]
     * @Author   kendy
     * @DateTime 2019-12-25T10:42:30+0800
     * @return   [type]                   [description]
     */
    public function getRoomDetail()
    {
        if (request()->isAjax()) {
            $postData = input('param.');
            $where = "room_id= {$postData['room_id']}";
            $roomModel = new hrm();
            $roomInfo = $roomModel->getRoomInfoById($where);
            $where0['id'] = ['IN', explode(',', $roomInfo['room_other'])];
            $where1['id'] = ['IN', explode(',', $roomInfo['room_fw_other'])];
            $where2['id'] = ['IN', explode(',', $roomInfo['food_other'])];
            $fw = $this->getDatasName($where0);
            $fw1 = $this->getDatasName($where1);
            $fw2 = $this->getDatasName($where2);
            if (count($fw) > 0) {
                foreach ($fw as $keys => $value) {
                    $room_other[] = $value['data_name'];
                    $roomInfo['room_other'] = implode(',', $room_other);
                }
            }
            if (count($fw1) > 0) {
                foreach ($fw1 as $keys => $value) {
                    $room_fw_other[] = $value['data_name'];
                    $roomInfo['room_fw_other'] = implode(',', $room_fw_other);
                }
            }
            if (count($fw2) > 0) {
                foreach ($fw2 as $keys => $value) {
                    $food_other[] = $value['data_name'];
                    $roomInfo['food_other'] = implode(',', $food_other);
                }
                return ['code' => 0, 'msg' => '', 'count' => 1, 'data' => $roomInfo];
            }
        }
    }

    public function getStatus($s)
    {
        switch ($s) {
            case 1 :
                return '是';
            case 0 :
                return '否';
        }
    }

    /**
     * 房间图片
     */
    public function viewImage()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $hotel_id = Session::get('hotel_id');
            $room_id = $post['room_id'];
            $where = ['room_id' => $room_id, 'hotel_id' => $hotel_id];
            $roomModel = new hrm();
            $data = $roomModel->getRoomImg($where);
            if ($data) {
                foreach ($data as $key => $v) {
                    $res [] = $v;
                }
                return json(['code' => 0, 'data' => $res]);

            } else {
                return ['code' => 1, 'message' => '暂无图片'];
            }
        }
    }

    public function edithotelInfo()
    {

        if (Request::instance()->isAjax()) {

            $post = Request::instance()->post();

            $post = $post['data'];
            $city = $post['province'] . $post['city'] . $post['district'];
            $eidtData = [

                'hotel_name' => $post['hotel_name'],
                'hotel_subject' => $post['hotel_subject'],
                'hotel_tel' => $post['hotel_tel'],
                'kind_type' => $post['kind_type'],
                //'should_konwn' => $post['should_konwn'],
                'hotel_tag' => $post['hotel_tag'],
                'address' => $post['address'],
                'city' => $city,
                // 'latitude'     => $post['latitude'],
                'latest_time' => $post['latest_time'],
            ];

            $res = hotelModel::editHotelInfo($eidtData);

            if ($res !== false) {

                return ['code' => 0];

            } else {

                return ['code' => 1];

            }

        }

    }

    /**
     * 景区封面图上传
     */
    public function uploadCoverImg()
    {
        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        // 上传到七牛后保存的文件名
        $key = 'lv/hotel/' . substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = config("qiniu")["accessKey"];
        $secretKey = config("qiniu")["secretKey"];

        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = config("qiniu")["bucket"];

        //域名
        $domain = config("qiniu")["domain"];

        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code" => 0, "msg" => $err, "data" => ""];
        } else {
            //返回图片的完整URL
            $imgPath = $domain . '/' . $key;

            hotelModel::editHotelInfo(['hotel_covers' => $imgPath]);

            return ["code" => 1, "msg" => "上传成功", "data" => $imgPath];
        }
    }

    public function getDatasName($where)
    {
        return Db::name('datas')->where($where)->select();
    }

}